package com.gin.kotlin.no_gay_alliance.provider.ngaphp

import com.gin.nga.main.NgaApi
import com.gin.nga.response.user.OldName
import java.lang.ref.WeakReference
import java.util.concurrent.TimeUnit

/**
 * 曾用名提供者
 */
class OldNameProvider private constructor() : AbstractCachedDataProvider<Long, List<List<OldName>>, List<OldName>>(
    javaType = TYPE_FACTORY.constructCollectionLikeType(List::class.java, OldName::class.java),
    expires = TimeUnit.DAYS.toSeconds(1)
) {
    override val dir = "OLD_NAME"
    override fun transform(rawData: List<List<OldName>>) = rawData.flatten()
    override fun requestMethod(ngaApi: NgaApi, params: Long?) = ngaApi.userApi.oldName(params!!)
    override fun obtainRelativePath(userId: Long, params: Long?) = "$params.json"

    companion object {
        private var instance: WeakReference<OldNameProvider> = WeakReference(OldNameProvider())
        fun getInstance(): OldNameProvider = instance.get() ?: OldNameProvider().also { instance = WeakReference(it) }
    }
}